<template>
	<view class='login'>
		<view class='login-from'>
			<view class='login-user'>
				<text class='user-text'>账号</text>
				<input type="text" v-model="userName" value="" placeholder="请输入手机号/昵称" />
			</view>
			<view class='login-user'>
				<text class='user-text'>密码</text>
				<input type="password" v-model="userPwd" value="" placeholder="6-16位字符" />
			</view>
		</view>
		<view class='login-quick'>
			<view>忘记密码?</view>
			<view>免密登录</view>
		</view>
		<button class='tel' @tap='wxLogin' type="primary"> 微信登录</button>
		<button class='tel' @tap='submit'> 登录</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userName: "",
				userPwd: "",
				registerData: {}
			};
		},
		methods: {
			// 微信登陆
			wxLogin() {
				uni.getUserInfo({ //1 获取用户信息
					desc: "登陆的数据",
					provider: 'weixin',
					success: (ures) => {
						console.log('用户昵称为222：', ures);
						console.log('用户昵称为444：', {
							...ures
						});
						console.log('用户昵称为：' + ures.userInfo.nickName);

						uni.login({ // 2 请求登陆接口
							success: (loginRes) => {
								let params = {
									code: loginRes.code
								}
								//1 获取用户信息
								//success: (res) => {
								// avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132"
								// city: ""
								// country: ""
								// gender: 0
								// language: ""
								// nickName: "微信用户"
								// province: ""
								//}
								// 2 请求登陆接口
								//模拟后端接口 
								// 60003  没有注册
								// 200 已经注册
								//3 前端请求后端 一键登陆接口 
								//3.1 把uni.login接口返回的code码给后端
								//3.2 后端会返回一个code码 这个code码是当前登陆用户是否注册过的

								//4 code码是60003的时候 我们要去注册
								// 4.1 调用后台给的注册接口 
								// 4.1.1 需要传递uni.getUserInfo(obj)返回用户信息
								// 2 注册成功 后端会返回code码 200 然后进入下一步
								// 5 code码是200的时候 我们要去判断用户是否绑定手机
								// 5.1

								uni.request({
									url: "http://172.20.10.4:3000/api/index_weix/data",
									header: {
										"Content-Type": "application/json;charset=utf-8"
										// "Content-Type":"application/x-www-form-urlencoded",
									},
									data: {
										code: params.code
									},
									method: "GET",
									dataType: "json",
									success: (res) => {
										console.log("resresreswwwwww" + res)
										// console.log("resresres" , JSON.stringify(res))
										let datas =res
										
											console.log("resresre2s" , datas)
											console.log("resresre43s" , datas.data.code)
											
											
										// let codetim = datas.data.code
										
										let {
											code,
											mobile
										} = datas.data
										
										
										//当前用户没有注册

										if (code == 60003) {
											
											this.registerData = {
												unionId: '123456',
												openId: '1234567',
												sessionKey: ures.signature,
												rawData: ures.rawData,
												encryptedData: ures.encryptedData,
												iv: ures.iv
											}
											this.register()
										}
										//当前用户注册了
										else if (code == 200) {
											this.mobile(mobile)

										}

									},
									fail: res => {
										console.log("res", res)
									},
									complete: res => {}
								})
								console.log("请求微信登陆的数据", loginRes);
							}

						})
					}

				});


				// uni.login({
				// 	provider: 'weixin',
				// 	success: (loginRes) => {
				// 		console.log("微信登陆", loginRes);
				// 		console.log("微信登陆ww", loginRes.authResult);
				// 		// 获取用户信息
				// 		uni.getUserInfo({
				// 			provider: 'weixin',
				// 			success: (infoRes) => {
				// 				console.log('用户昵称为222：' + infoRes);

				// 				console.log('用户昵称为：' + infoRes.userInfo.nickName);
				// 			}
				// 		});
				// 	}
				// });

			},

			// 微信一键登陆
			wxinlogo() {

				uni.request({
					url: "http://172.20.10.4:3000/api/index_list/data",
					header: {
						"Content-Type": "application/json;charset=utf-8"
						// "Content-Type":"application/x-www-form-urlencoded",
					},
					data: {},
					method: "GET",
					dataType: "json",

					success: (res) => {
						console.log("resresres" + JSON.stringify(res))

					},
					fail: res => {
						console.log("res", res)
					},
					complete: res => { }
				})

			},
			//新用户注册
			register() {
					console.log("注册成功0988");
				let registerData = this.registerData
				uni.request({
					url: "http://172.20.10.4:3000/api/tab_one/data",
					header: {
						"Content-Type": "application/json;charset=utf-8"
						// "Content-Type":"application/x-www-form-urlencoded",
					},
					data: {
						"restime": registerData
					},
					method: "GET",
					dataType: "json",
					success: (res) => {
						let datas = res
						let codetim = datas.data.code
						if (codetim === 200) {

							console.log("注册成功");
						} else {
							console.log("注册失败，重新注册");
						}
						console.log("resresres" + JSON.stringify(res))
					},
					fail: res => {
						console.log("res", res)
					},
					complete: res => {}
				})

			},
			//返回code是200是，保持toke 然后判断是否绑定了手机号码
			mobile(data) {
				// 保持用户toke
				uni.setStorageSync("toke", "95279527")
				uni.getStorageSync("toke")
				if (!data) {
					//没有绑定 就应该去绑定手机号页面
					this.showMobile()
					return
				} else {
					uni.switchTab({
						url: "/pages/index/index"
					})
				}
			},

			//调转到手机登陆界面
			showMobile() {
				uni.showModal({
					title: "提示",
					content: "更具国家法定法规，需要绑定手机号",
					confirmText: "去绑定",
					success: (res) => {
						console.log("res", res);
						if(res.confirm){ //去绑定
							uni.navigateTo({
								url: "/pagesA/iPhones/iPhones"
							})
						}else{
							uni.removeStorageSync("toke")
						}
					}
				})
			},
			// 登陆
			submit() {

			},
		}
	}
</script>

<style lang="scss">
	.logo {
		padding-bottom: 100rpx;
		display: flex;
		justify-content: center;
	}

	.login-from {
		padding-top: 100rpx;
	}

	.login-user {
		font-size: 32rpx;
		padding: 10rpx 0;
		display: flex;
		align-items: center;
		border-bottom: 2rpx solid #f7f7f7;
	}

	.user-text {
		padding-right: 10rpx;
	}

	.login-quick {
		display: flex;
		justify-content: space-between;
		padding: 20rpx 0;
	}

	.reminder {
		color: #CCCCCC;
		padding: 20rpx 0;
		text-align: center;
	}
</style>